// shareWorker.js
let clients = new Set();

self.onconnect = function(event) {
  const port = event.ports[0];
  clients.add(port);

  port.onmessage = function(event) {
    console.log('Received message from client:', event.data);

    // 广播消息给所有连接的客户端
    clients.forEach(client => {
      client.postMessage('Hello, all clients!');
    });
  };

  port.onmessageerror = function(error) {
    console.error('Message error:', error);
  };

  port.onclose = function() {
    console.log('Port closed');
    clients.delete(port);
  };
};